//
//  WelcomeLayer.cpp
//  firebkball
//
//  Created by ren jian on 18/5/28.
//
//

#include "WelcomeLayer.h"

USING_NS_CC;
using namespace cocos2d::ui;

#include "SimpleAudioEngine.h"

#include "HelloWorldScene.h"
#include "WinnerLayer.h"
#include "FailureLayer.h"
#include "MyFunction.h"
#include "SettingView.h"

//TODO::测试jni
// #include "MotherboardConnector.h"


Scene *m_scene;

Scene* WelcomeLayer::createScene()
{
    m_scene = Scene::create();
    m_scene->addChild(WelcomeLayer::create());
    return m_scene;
}

bool WelcomeLayer::init()
{
    if (!Layer::init()) {
        return false;
    }
    CCLOG("初始化...");
    
    UserDataManager::getInstance();
    
    m_size = Director::getInstance()->getWinSize();
    this->ignoreAnchorPointForPosition(false);
    this->setAnchorPoint(Vec2(0.5, 0.5));
    this->setContentSize(m_size);
    this->setPosition(m_size/2);
    
    m_controller = CSLoader::createNode("res/welcomeView.csb");
    m_controller->setAnchorPoint(Vec2(0.5, 0.5));
    m_controller->setPosition(m_size/2);
    this->addChild(m_controller);
    
    // 投币节点
    m_inCoinNode = m_controller->getChildByName("nd_toubi");
    m_inCoinNode->setVisible(false);
    // 开始节点
    m_startNode = (cocos2d::ui::Button *)m_controller->getChildByName("btn_start");
    m_startNode->setVisible(true);
    m_startNode->setEnabled(true);
    m_startNode->setTouchEnabled(true);
    m_startNode->runAction(RepeatForever::create(Sequence::create(
                                                                  ScaleTo::create(0.2, 0.9),
                                                                  EaseBackOut::create(ScaleTo::create(0.2, 1)),
                                                                  NULL)));
    m_startNode->addClickEventListener(CC_CALLBACK_1(WelcomeLayer::startGame, this));
    
    // 初始化分数榜
    this->initScoreTop();
    // 初始化投币数
    this->changeCoin();
    
    // TODO::测试
    auto btn_addCoin = (cocos2d::ui::Button *)m_controller->getChildByName("btn_addCoin");
    btn_addCoin->addClickEventListener(CC_CALLBACK_1(WelcomeLayer::testAddCoin, this));
    auto btn_setting = (cocos2d::ui::Button *)m_controller->getChildByName("btn_setting");
    btn_setting->addClickEventListener(CC_CALLBACK_0(WelcomeLayer::goSettingScene, this));
    
    return true;
}

void WelcomeLayer::onEnter()
{
    Layer::onEnter();
    CCLOG("on enter...");
    this->addCoin(0);
    
    if (!CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying()) {
        CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("audio/bg.mp3", true);
    }
}

void WelcomeLayer::onExit()
{
    Layer::onExit();
    CCLOG("on exit...");
}

void WelcomeLayer::initScoreTop()
{
    auto l_topNodes = m_controller->getChildByName("box_topRole")->getChildren();
    for (int i=0; i<m_controller->getChildByName("box_topRole")->getChildrenCount(); i++) {
        auto l_topNode = (cocos2d::ui::Text*)l_topNodes.at(i);
        l_topNode->setString(StringUtils::format("%d", UserDataManager::getInstance()->userData->scoreTop[i]));
    }
}

void WelcomeLayer::goSettingScene()
{
    Director::getInstance()->replaceScene(SettingView::createScene());
}

void WelcomeLayer::changeCoin()
{
    auto lab_coinNumber = (cocos2d::ui::Text*)m_inCoinNode->getChildByName("lab_coinNumber");
    string l_inCoin = StringUtils::format("%d / %d", UserDataManager::getInstance()->userData->coin, UserDataManager::getInstance()->userData->startCoinNumber);
    lab_coinNumber->setString(l_inCoin);
}

void WelcomeLayer::testAddCoin(cocos2d::Ref *ref)
{
    this->addCoin(1);
}

void WelcomeLayer::addCoin(int _number)
{
    UserDataManager::getInstance()->addCoin(_number);
    this->changeCoin();
    CCLOG("投币数: %d", UserDataManager::getInstance()->userData->coin);
    
    m_inCoinNode->setVisible(UserDataManager::getInstance()->userData->coin<UserDataManager::getInstance()->userData->startCoinNumber);
    m_startNode->setVisible(UserDataManager::getInstance()->userData->coin>=UserDataManager::getInstance()->userData->startCoinNumber);
}

void WelcomeLayer::startGame(Ref *ref)
{
    CCLOG("开始游戏");
    if (UserDataManager::getInstance()->userData->coin<UserDataManager::getInstance()->userData->startCoinNumber) {
        this->addCoin(0);
        return;
    }
    UserDataManager::getInstance()->addCoin(-UserDataManager::getInstance()->userData->startCoinNumber);
    Director::getInstance()->replaceScene(HelloWorld::createScene());
}

void WelcomeLayer::onTouchEnded(Touch *touch, Event *unused_event)
{
    CCLOG("on onTouchEnded...");
}

